package com.sevattal.service.leaves.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sevattal.common.base.result.ResultCodeEnum;
import com.sevattal.service.base.exception.SevattalException;
import com.sevattal.service.leaves.entity.PurchaseChildOrder;
import com.sevattal.service.leaves.entity.PurchaseMainOrder;
import com.sevattal.service.leaves.entity.vo.AuditOrderVo;
import com.sevattal.service.leaves.entity.vo.PurchaseChildOrderVo;
import com.sevattal.service.leaves.entity.vo.PurchaseMainOrderVo;
import com.sevattal.service.leaves.entity.vo.PurchaseOrderVo;
import com.sevattal.service.leaves.mapper.PurchaseChildOrderMapper;
import com.sevattal.service.leaves.mapper.PurchaseMainOrderMapper;
import com.sevattal.service.leaves.service.PurchaseOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  采购主表 服务实现类
 * </p>
 *
 * @author Sevattal
 * @since 2021-08-28
 */
@Service
public class PurchaseOrderServiceImpl implements PurchaseOrderService {

    @Autowired
    private  PurchaseMainOrderMapper mainOrderMapper;

    @Autowired
    private PurchaseChildOrderMapper childOrderMapper;

    @Override
    public boolean auditOrder(AuditOrderVo auditOrderVo) {
        QueryWrapper<PurchaseMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.eq("f_pmId",auditOrderVo.getOrderId());
        PurchaseMainOrder mainOrder = new PurchaseMainOrder();
        mainOrder.setFAuditid(auditOrderVo.getAuditId());
        mainOrder.setFPmstatus(auditOrderVo.getAuditStatus());
        int count = mainOrderMapper.update(mainOrder,mainOrderQueryWrapper);
        return count == 1;
    }

    @Override
    public List<PurchaseOrderVo> getPurchaseOrderList(PurchaseOrderVo purchaseOrderVo) {
        QueryWrapper<PurchaseMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.eq("pm.is_disabled",0);
        mainOrderQueryWrapper.eq("pm.is_deleted",0);
        mainOrderQueryWrapper.orderByAsc("pm.f_pmId");
        if (purchaseOrderVo.getFPmid() != null && !StringUtils.isEmpty(purchaseOrderVo.getFPmid())){
            mainOrderQueryWrapper.like("pm.f_pmId",purchaseOrderVo.getFPmid());
        }
        if (purchaseOrderVo.getFEname() != null && !StringUtils.isEmpty(purchaseOrderVo.getFEname())){
            mainOrderQueryWrapper.like("e1.f_eName",purchaseOrderVo.getFEname());
        }
        if (purchaseOrderVo.getFWhid() != null && !StringUtils.isEmpty(purchaseOrderVo.getFWhid())){
            mainOrderQueryWrapper.eq("pm.f_whId",purchaseOrderVo.getFWhid());
        }
        if (purchaseOrderVo.getFPname() != null && !StringUtils.isEmpty(purchaseOrderVo.getFPname())){
            mainOrderQueryWrapper.like("p.f_pName", purchaseOrderVo.getFPname());
        }
        if (purchaseOrderVo.getSearchStartDate() != null){
            mainOrderQueryWrapper.ge("pm.f_pmDate",purchaseOrderVo.getSearchStartDate());
        }
        if (purchaseOrderVo.getSearchEndDate() != null){
            mainOrderQueryWrapper.le("pm.f_pmDate",purchaseOrderVo.getSearchEndDate());
        }
        List<PurchaseMainOrderVo> purchaseMainOrderVos = mainOrderMapper.getPurchaseMainOrderList(mainOrderQueryWrapper);
        List<PurchaseOrderVo> purchaseOrderVoList = new ArrayList<>();
        QueryWrapper<PurchaseChildOrder> childOrderQueryWrapper = new QueryWrapper<>();
        for (int i = 0;i<purchaseMainOrderVos.size();i++){
            childOrderQueryWrapper.eq("pc.f_pmId",purchaseMainOrderVos.get(i).getFPmid());
            childOrderQueryWrapper.eq("pc.is_disabled","0");
            childOrderQueryWrapper.eq("pc.is_deleted","0");
            List<PurchaseChildOrderVo> purchaseChildOrderVos = childOrderMapper.getPurchaseChildOrderList(childOrderQueryWrapper);
            purchaseOrderVoList.add(getPurchaseOrderVo(purchaseMainOrderVos.get(i),purchaseChildOrderVos));
            childOrderQueryWrapper.clear();
        }
        return purchaseOrderVoList;
    }

    @Override
    public boolean deletePurchaseOrder(String id) {
        QueryWrapper<PurchaseMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.eq("f_pmId",id);
        QueryWrapper<PurchaseChildOrder> childOrderQueryWrapper = new QueryWrapper<>();
        childOrderQueryWrapper.eq("f_pmId",id);
        mainOrderMapper.delete(mainOrderQueryWrapper);
        childOrderMapper.delete(childOrderQueryWrapper);
        return true;
    }

    @Override
    public boolean updatePurchaseOrder(PurchaseOrderVo purchaseOrderVo) {
        JudgmentData(purchaseOrderVo);
        QueryWrapper<PurchaseMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.eq("f_pmId",purchaseOrderVo.getFPmid());
        int count = mainOrderMapper.selectCount(mainOrderQueryWrapper);
        if (count == 0 ){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        mainOrderMapper.update(getPurchaseMainOrder(purchaseOrderVo),mainOrderQueryWrapper);

        QueryWrapper<PurchaseChildOrder> childOrderQueryWrapper =new QueryWrapper<>();
        childOrderQueryWrapper.eq("f_pmId",purchaseOrderVo.getFPmid());
        childOrderMapper.delete(childOrderQueryWrapper);
        List<PurchaseChildOrder> purchaseChildOrderList = getPurchaseChildOrder(purchaseOrderVo);
        for (int i = 0; i<purchaseChildOrderList.size();i++){
            childOrderMapper.insert(purchaseChildOrderList.get(i));
        }
        return true;
    }

    @Override
    public boolean createPurchaseOrder(PurchaseOrderVo purchaseOrderVo) {
        JudgmentData(purchaseOrderVo);
        QueryWrapper<PurchaseMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.eq("f_pmId",purchaseOrderVo.getFPmid());
        int count = mainOrderMapper.selectCount(mainOrderQueryWrapper);
        if (count > 0 ){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        mainOrderMapper.insert(getPurchaseMainOrder(purchaseOrderVo));
        List<PurchaseChildOrder> purchaseChildOrderList = getPurchaseChildOrder(purchaseOrderVo);
        for (int i = 0; i<purchaseChildOrderList.size();i++){
            childOrderMapper.insert(purchaseChildOrderList.get(i));
        }
        return true;
    }


    // 判断数据是否正确
    public void JudgmentData(PurchaseOrderVo purchaseOrderVo){
        if (purchaseOrderVo.getFPmid() == null || StringUtils.isEmpty(purchaseOrderVo.getFPmid()) ||
        purchaseOrderVo.getPurchaseChildOrderVoList().size() == 0 ){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
    }

    // 获取与主表
    public PurchaseMainOrder getPurchaseMainOrder(PurchaseOrderVo purchaseOrderVo){
        PurchaseMainOrder purchaseMainOrder = new PurchaseMainOrder();
        purchaseMainOrder.setFPmid(purchaseOrderVo.getFPmid());
        purchaseMainOrder.setFEid(purchaseOrderVo.getFEid());
        purchaseMainOrder.setFAuditid(purchaseOrderVo.getFAuditid());
        purchaseMainOrder.setFPid(purchaseOrderVo.getFPid());
        purchaseMainOrder.setFWhid(purchaseOrderVo.getFWhid());
        purchaseMainOrder.setFPmdate(purchaseOrderVo.getFPmdate());
        purchaseMainOrder.setFPmstatus(purchaseOrderVo.getFPmstatus());
        purchaseMainOrder.setFPmconfirm(purchaseMainOrder.getFPmconfirm());
        purchaseMainOrder.setFPmmemo(purchaseMainOrder.getFPmmemo());
        return  purchaseMainOrder;
    }
    // 获取子表信息
    public List<PurchaseChildOrder> getPurchaseChildOrder(PurchaseOrderVo purchaseOrderVo){
            List<PurchaseChildOrder> purchaseChildOrderList = new ArrayList<>();
            List<PurchaseChildOrderVo> purchaseChildOrderVos = purchaseOrderVo.getPurchaseChildOrderVoList();
            PurchaseChildOrder child_temp = new PurchaseChildOrder();
            for (int i = 0; i< purchaseChildOrderVos.size(); i++ ){
                child_temp.setFPmid(purchaseChildOrderVos.get(i).getFPmid());
                child_temp.setFMid(purchaseChildOrderVos.get(i).getFMid());
                child_temp.setFPcquantity(purchaseChildOrderVos.get(i).getFPcquantity());
                child_temp.setFPcunitprice(purchaseChildOrderVos.get(i).getFPcunitprice());
                child_temp.setFPcdate(purchaseChildOrderVos.get(i).getFPcdate());
                child_temp.setFPcstatus(purchaseChildOrderVos.get(i).getFPcstatus());
                child_temp.setFPcmemo(purchaseChildOrderVos.get(i).getFPcmemo());
                purchaseChildOrderList.add(child_temp);
            }
            return  purchaseChildOrderList;
    }
    // 生成VO信息
    public PurchaseOrderVo getPurchaseOrderVo(PurchaseMainOrderVo mainOrderVo, List<PurchaseChildOrderVo> childOrderVos){
        PurchaseOrderVo purchaseOrderVo = new PurchaseOrderVo();
        purchaseOrderVo.setFPmid(mainOrderVo.getFPmid());
        purchaseOrderVo.setFEid(mainOrderVo.getFEid());
        purchaseOrderVo.setFEname(mainOrderVo.getFEname());
        purchaseOrderVo.setFAuditid(mainOrderVo.getFAuditid());
        purchaseOrderVo.setFAuditname(mainOrderVo.getFAuditname());
        purchaseOrderVo.setFPid(mainOrderVo.getFPid());
        purchaseOrderVo.setFPname(mainOrderVo.getFPname());
        purchaseOrderVo.setFWhid(mainOrderVo.getFWhid());
        purchaseOrderVo.setFWhname(mainOrderVo.getFWhname());
        purchaseOrderVo.setFPmdate(mainOrderVo.getFPmdate());
        purchaseOrderVo.setFPmstatus(mainOrderVo.getFPmstatus());
        purchaseOrderVo.setFPmconfirm(mainOrderVo.getFPmconfirm());
        purchaseOrderVo.setFPmmemo(mainOrderVo.getFPmmemo());
        purchaseOrderVo.setPurchaseChildOrderVoList(childOrderVos);
        return purchaseOrderVo;
    }
}
